package com.wisdytech.linkdcs.partLife.service.impl;

import com.wisdytech.linkdcs.partLife.model.ModelPart;
import com.wisdytech.linkdcs.partLife.model.Part;
import com.wisdytech.common.bean.Filter;
import com.wisdytech.common.exception.ServiceException;
import com.wisdytech.common.handler.BaseService;
import com.wisdytech.common.utils.BaseUtil;
import com.wisdytech.linkdcs.equipment.dao.IEqEquipmentDao;
import com.wisdytech.linkdcs.equipment.dao.IEqModelDao;
import com.wisdytech.linkdcs.equipment.model.Equipment;
import com.wisdytech.linkdcs.partLife.dao.IModelPartDao;
import com.wisdytech.linkdcs.partLife.dao.IPartDao;
import com.wisdytech.linkdcs.partLife.service.IModelPartService;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.github.pagehelper.PageInfo;

import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * <p>
 *  设备型号备件服务实现类
 * </p>
 *
 * @author chenbin
 * @since 2018-06-05
 */
@Service("iModelPartService")
@Transactional
public class ModelPartServiceImpl extends BaseService<IModelPartDao, ModelPart> implements IModelPartService {

    @Resource
    private IModelPartDao iModelPartDao;
	@Resource
	private IPartDao iPartDao;
    /**
     * 注入设备类别dao
     */
    @Resource
    private IEqModelDao eqModelDao;
    /**
     * 注入设备dao
     */
    @Resource
    private IEqEquipmentDao equipDao;
    
	@Override
	public PageInfo<ModelPart> listModelPartByFilter(String modelCode, Filter filter) {
        if(StringUtils.isBlank(modelCode))
            return new PageInfo<>(new ArrayList<>());
        List<ModelPart> list = this.beforeList(IModelPartDao.class,filter).listModelPartByFilter(modelCode);
        PageInfo<ModelPart> page = new PageInfo<>(list);
        return page;
	}

	@Override
	public void addPartConfigInfo(ModelPart modelPart) {
		EntityWrapper<ModelPart> ew = new EntityWrapper<>();
        //验证备件编码的唯一性
        List<ModelPart> modelPartList = new ArrayList<>();
        modelPartList = iModelPartDao.selectPage(new Page<ModelPart>(1, 1), ew.eq("LOCATION_CODE", modelPart.getLocationCode()));
        if (!modelPartList.isEmpty()) {
        	throw new ServiceException("该备件位置编码被使用");
        }
        modelPart.setPkId(BaseUtil.getUUID());
        int partCount = iModelPartDao.insert(modelPart);
        if (partCount <= 0) {
            throw new ServiceException("新增失败");
        }
		
	}

	@Override
	public ModelPart getPartConfigInfoById(String pkId) {
		ModelPart modelPart = iModelPartDao.selectById(pkId);
		if(modelPart!=null) {
			Part part = iPartDao.selectById(modelPart.getPartId());
			modelPart.setPartCode(part.getPartCode());
			modelPart.setPartName(part.getPartName());
			modelPart.setPartType(part.getPartType());
		}
		return modelPart;
	}

	@Override
	public void updatePartConfigInfo(ModelPart modelPart) {
		EntityWrapper<ModelPart> ew = new EntityWrapper<>();;
        //验证备件编码的唯一性--修改功能
		List<ModelPart> modelPartList;
		modelPartList = iModelPartDao.selectPage(new Page<ModelPart>(1, 1),ew.eq("LOCATION_CODE", modelPart.getLocationCode()).ne("pk_id", modelPart.getPkId()));
        if (modelPartList.size() > 0){
        	throw new ServiceException("该备件位置编码被使用");
        }
//        //验证验证是否已经添加
//        EntityWrapper<ModelPart> ew1 = new EntityWrapper<>();
//        List<ModelPart> modelPList = new ArrayList<>();
//        modelPList = iModelPartDao.selectPage(new Page<ModelPart>(1, 1), ew1.eq("PART_ID", modelPart.getPartId()).eq("MODEL_CODE", modelPart.getModelCode()).ne("pk_id", modelPart.getPkId()));
//        if (!modelPList.isEmpty()) {
//        	throw new ServiceException("该备件已被添加");
//        }
        int operationCount = iModelPartDao.updateById(modelPart);
        if (operationCount <= 0) {
            throw new ServiceException("修改失败");
        }
		
	}

	@Override
	public void deletePartConfigById(String ids) {
        List<String> idList = new ArrayList<String>();
        String[] id = ids.split(",");
        for (int i = 0; i < id.length; i++) {
            idList.add(id[i]);
        }
        int partCount = iModelPartDao.deleteBatchIds(idList);
        if (partCount <= 0) {
            throw new ServiceException("删除失败");
        }
		
		
	}

	@Override
	public List<ModelPart> listlmodelPartByPartId(String search,String partId, String equipCode) {
		
		Equipment equip = equipDao.selectByEquipCode(equipCode);
		EntityWrapper<ModelPart> ew = new EntityWrapper<>();
		ew.like("LOCATION_CODE", search).eq("PART_ID", partId).eq("MODEL_CODE", equip.getModelCode());
		return iModelPartDao.selectList(ew);
	}

	@Override
	public ModelPart getModelPartByLocation(String locationCode) {
		ModelPart modelPart = new ModelPart();
		modelPart.setLocationCode(locationCode);
		return iModelPartDao.selectOne(modelPart);
	}

	@Override
	public ModelPart getModelPartIdByPkIdAndEqId(String localtionCode) {
		ModelPart modelPart = new ModelPart();
		modelPart.setLocationCode(localtionCode);
		return iModelPartDao.selectOne(modelPart);
	}

}
